import {Injectable} from '@angular/core';
import {AppConfig} from '../config';
import {MenuParam} from '../entity/menu-param.model';
import {Observable} from 'rxjs';
import {InterceptorService} from './interceptor/interceptor-service';

/**
 * 菜单参数
 *
 * @export
 * @class MenuParamService
 */
@Injectable()
export class MenuParamService {
  private param = 'menuParams';

  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(
    private http: InterceptorService,
  ) {
  }

  /**
   * 获得所有菜单参数列表
   *
   * @returns {Promise<MenuParam[]>}
   * @memberof MenuParamService
   */
  getMenuParams(): Promise<MenuParam[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.data as MenuParam[])
      .catch(this.handleError);
  }

  /**
   * 根据菜单Id获得菜单下菜单参数
   *
   * @param {number} menuId
   * @returns {Promise<MenuParam[]>}
   * @memberof MenuParamService
   */
  getMenuParamsByMenuId(menuId: number): Promise<MenuParam[]> {
    return this.http
      .get(`${this.url}/menuId/${menuId}`)
      .toPromise()
      .then(response => response.data as MenuParam[])
      .catch(this.handleError);
  }

  /**
   * 创建菜单参数
   *
   * @param {MenuParam} menuParam
   * @returns {Promise<MenuParam>}
   * @memberof MenuParamService
   */
  create(menuParam: MenuParam): Promise<MenuParam> {
    return this.http
      .post(this.url, JSON.stringify(menuParam))
      .toPromise()
      .then(result => {
        try {
          switch (result.code) {
            case '200':
              return result.data as MenuParam;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }

  /**
   * 更新菜单参数
   *
   * @param {MenuParam} menuParam
   * @returns {Promise<MenuParam>}
   * @memberof MenuParamService
   */
  update(menuParam: MenuParam): Promise<MenuParam> {
    const url = `${this.url}`;
    return this.http.put(url, JSON.stringify(menuParam))
      .toPromise()
      .then(() => menuParam)
      .catch(this.handleError);
  }

  /**
   * 批量删除
   *
   * @param {number[]} paramIds
   * @returns {Promise<boolean>}
   * @memberof MenuParamService
   */
  batchDelete(paramIds: number[]): Promise<boolean> {
    const apiUrl = `${this.url}?items=${paramIds}`;
    return this.http
      .delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response as any;
          switch (result.code) {
            case '200':
              return true;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }

  /**
   * 在同一菜单下是否存在相同的菜单参数名称
   *
   * @param {string} paramName
   * @param {number} menuId
   * @param {number} paramId
   * @returns {Observable<boolean>}
   * @memberof MenuParamService
   */
  hasMenuParamByParamname(paramName: string, menuId: number, paramId: number): any {
    const url = `${this.url}/paramName/${paramName}/menuId/${menuId}/paramId/${paramId}`;
    return this.http.get(url).map(response => {
      let code = response.code;
      switch (code) {
        case '3001':
          return true;
        case '3002':
          return false;
      }
    }, this.handleError)
  }

  private handleError(error: any): Promise<any> {
    // console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }
}


